home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
STANDALO
/
BOUNCE_C
/
BOUNCE.C
next >
Wrap
Text File
|
1991-01-25
|
4KB
|
191 lines
/*
* bounce.c -- Bounce's main code section.
*
* Bounce is a cdev that displays a bouncing line in the control panel. When you
* select the about button, it puts up a dialog that also contains a bouncing line,
* using the same code for drawing both lines.
*
* This illustrates that cdevs are re-entrant, i.e. an executing cdev can indirectly
* call itself by using Alert, ModalDialog, SFGetFile, or any other function which
* has an event loop. Also, this cdev does its work during null event time, so it
* will continue to draw in a background MultiFinder layer.
*
* Since the about box code uses SetUpA4, it isn't truly re-entrant, because SetUpA4
* only contains one copy of A4. This doesn't affect us, since the about box code
* itself isn't called re-entrantly, and A4 would be the same in any case (using
* current system software, at least!).
* by Phil Shapiro. Copyright ⌐ 1990,1991 Symantec Corp. All rights reserved.
*/
#include "bounce.h"
/*
* This useful library contains a function which centers a dialog box on the
* screen that contains the mouse. It's used for the about box.
*/
#include "DialogUtils.h"
/*
* Runnable - should the cdev appear in the Control Panel?
*
* This implements the "macDev" message.
*
*/
Boolean Runnable()
{
return(true);
}
/*
* New - create the cdev object.
*/
cdev * New()
{
return((cdev *)new(Bounce));
}
/*
* If you don't call the inherited method, you *MUST* return 'this' or you will
* die the next time the cdev is called.
*/
long Bounce::Message(int msg, int item)
{
if (msg == cursorDev)
Cursor();
return inherited::Message(msg, item);
}
/*
* Initialize the cdev. First load in my cursor, then allocate the LineBox.
*/
void Bounce::Init()
{
int type;
Rect box;
Handle hdl;
cursor = ReadRsrc('CURS', CURSID);
if (lines = new(LineBox)) {
GetDItem((DialogPtr)dp, lastItem + BounceItem, &type, &hdl, &box);
if (lines->Init(NUM_LINES, &box, (GrafPtr)dp)) {
inherited::Init();
return;
}
delete(lines);
}
else
Error(cdevMemErr);
}
/*
* Cleanup by closing the lines and deleting it.
*/
void Bounce::Close()
{
lines->Close();
delete(lines);
inherited::Close();
}
/*
* Just tell the lines to draw itself.
*/
void Bounce::Update()
{
lines->Draw();
inherited::Update();
}
/*
* The only item which does anything is the '?' button. This brings up the
* about box.
*/
void Bounce::ItemHit(int item)
{
Point where = event->where;
switch (item) {
case TitleItem:
break;
case AboutItem:
About();
break;
case BounceItem:
break;
default:
inherited::ItemHit(item);
}
}
/*
* During idle time, call the lines Idle routine. This animates the line.
*/
void Bounce::Idle()
{
lines->Idle();
inherited::Idle();
}
/*
* This will let my cdev use the arrow cursor. It's stored in a resource since cdevs
* have no qd globals.
*/
void Bounce::Cursor()
{
if (!cursor)
return;
SetCursor((Cursor *)*cursor);
}
/*
* This routine prepares to bring up the about box. Typically this would be an ALRT,
* but I need the bounding box for the AboutBounceItem, and there's no (easy) way to
* get at that information from an ALRT's DITL. Also, it's harder to preload and
* set the refCon of an ALRT than a DLOG. The refCon is used to pass the aboutLines
* object to the dialog's filter proc. The type and box args are used by BounceAbout
* to set the UserItem's draw procedure.
*/
void Bounce::About()
{
LineBox *aboutLines;
DialogPtr aboutBox;
int type;
Rect box;
Handle hdl;
int itemHit;
aboutBox = (DialogPtr) GetNewDialog(DU_CenterDLOG(AboutID), NIL, (WindowPtr)-1);
if (!aboutBox)
return;
/* If allocating the LineBox fails, we skip the about box. */
if (aboutLines = new(LineBox)) {
GetDItem(aboutBox, AboutBounceItem, &type, &hdl, &box);
if (aboutLines->Init(NUM_LINES, &box, (GrafPtr)aboutBox)) {
SetWRefCon((WindowPtr) aboutBox, (long) aboutLines);
BounceAbout(aboutBox, type, &box);
aboutLines->Close();
}
delete(aboutLines);
}
DisposDialog(aboutBox);
}
/*
* ReadRsrc -- read a resource from the cdev's file and set the error codes
* if anything goes wrong. Otherwise, set the non-purge bit.
*/
Handle Bounce::ReadRsrc(OSType type, int number)
{
Handle rsrc;
int refNum;
rsrc = Get1Resource(type, number);
if (!rsrc || (ResError() != noErr)) {
Error(cdevResErr);
if (rsrc)
ReleaseResource(rsrc);
return NIL;
}
HNoPurge(rsrc);
return rsrc;
}